// pages/address/address.js
import WxValidate from "../../utils/wxValidate.js";
var api = require("../../utils/api");
const app = getApp();
Page({
  /**
   * 页面的初始数据 
   */
  data: {
    addrs: [],
    // 默认
    addrMr: [],
    //提交订单页面传参
    show: '',
    startX: 0, //开始坐标
    startY: 0,
    bottom: '',
  },
  /**
   * 生命--监听页面加载
   */
  onLoad: function (options) {
    this.addrs();
    this.setData({
      show: options.type,
      type: options.type,
      bottom: app.globalData.isFullSucreen ? '40rpx' : '0',
    })
  },
  handleReplace(e) {
  },
  addrs() {
    let addrs = this.data.addrs;
    api.handleAddr({
      openId: wx.getStorageSync("openId"),
      cityId: wx.getStorageSync("cityId"),
      addressType: 2
    })
      .then(res => {
        let arr = res.object;
        if (res.result && res.object.length != 0) {
          this.setData({
            addrs: res.object
          })
        }
      })
  },
  edit: function () {
    wx.navigateTo({
      url: '../edit-address/edit-address?newAdd=' + 1 + '&type=' + this.data.type,
    })
  },
  addrShop(e) {
    let that = this;
    let index = e.currentTarget.dataset.index;
    if (this.data.show == 1) {
      let pages = getCurrentPages(e);
      let prevPage = pages[pages.length - 2];
      let addrs = that.data.addrs;
      let addrMess = null;
      addrs.forEach((v, i) => {
        if (i === index) {
          addrMess = {
            buildNo: v.buildNo,//详细地址
            addr: v.addr,//地址
            takePerson: v.takePerson,//收货人
            linktel: v.linktel,//电话
            latitude: v.latitude,//纬度
            longitude: v.longitude,//经度
            cityCode: v.cityCode,//区码
            cityAddr: v.cityAddr
          }
        }
      });
      prevPage.setData({
        addrMess
      })
      wx.navigateBack({
        delta: 1
      })
    } else {
      let addrs = that.data.addrs;
      let addrsIndex = addrs.findIndex((v, i) => i == index);
      let addr = addrs[addrsIndex].addr;//收货地址
      let buildNo = addrs[addrsIndex].buildNo//详细地址
      let takePerson = addrs[addrsIndex].takePerson//收货人
      let linktel = addrs[addrsIndex].linktel//联系电话
      let id = addrs[addrsIndex].id
      //  let flag = addrs[addrsIndex].flag
      wx.navigateTo({
        url: '/pages/personShow/personShow?addrs=' + addr + '&buildNo=' + buildNo + '&takePerson=' + takePerson + '&linktel=' + linktel + '&id=' + id
      })
    }
  },


  //手指触摸动作开始 记录起点X坐标
  touchstart: function (e) {
    //开始触摸时 重置所有删除
    let addrs = this.data.addrs
    addrs.forEach(function (v, i) {
      if (v.isTouchMove)//只操作为true的
        v.isTouchMove = false;
    })
    this.setData({
      startX: e.changedTouches[0].clientX,
      startY: e.changedTouches[0].clientY,
      addrs,
    })
  },

  //滑动事件处理

  touchmove: function (e) {
    var that = this,
      index = e.currentTarget.dataset.index,//当前索引
      startX = that.data.startX,//开始X坐标
      startY = that.data.startY,//开始Y坐标
      touchMoveX = e.changedTouches[0].clientX,//滑动变化坐标
      touchMoveY = e.changedTouches[0].clientY,//滑动变化坐标
      //获取滑动角度
      angle = that.angle({ X: startX, Y: startY }, { X: touchMoveX, Y: touchMoveY });
    let addrs = that.data.addrs
    addrs.forEach(function (v, i) {
      v.isTouchMove = false
      //滑动超过30度角 return
      if (Math.abs(angle) > 30) return;
      if (i == index) {
        if (touchMoveX > startX) //右滑
          v.isTouchMove = false
        else //左滑
          v.isTouchMove = true
      }
    })
    //更新数据
    that.setData({
      addrs,
    })
  },
  // * 计算滑动角度
  angle: function (start, end) {
    var _X = end.X - start.X,
      _Y = end.Y - start.Y
    return 360 * Math.atan(_Y / _X) / (2 * Math.PI);
  },
  // 删除
  del: function (e) {
    let id = e.currentTarget.dataset.id;
    let index = e.currentTarget.dataset.index;
    let addrs = this.data.addrs;
    api.personDel({
      id:id
    }).then(res=>{
      addrs.splice(index, 1)
      this.setData({
        addrs
      })
    })

  }
})